package com.kzw.oa.blog.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kzw.core.bean.Msg;
import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.util.json.JSON2;
import com.kzw.core.util.web.ResponseUtils;
import com.kzw.oa.blog.model.Article;
import com.kzw.oa.blog.service.ArticleService;
import com.kzw.oa.comm.model.TongBao;
import com.kzw.oa.comm.service.TongBaoService;
import com.kzw.oa.system.model.AppUser;
import com.kzw.oa.system.model.Dictionary;

@Controller
@RequestMapping("/blog")
public class BlogAction {
	
	@Autowired
	private TongBaoService tongbaoService;
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping({"", "index"})
	public String index(Model model) {
		
		List<TongBao> list = tongbaoService.listMy();
		if(list.size() > 0) {
			model.addAttribute("tb", list.get(0));
		}
		
		return "blog/index";
	}
	
	@RequestMapping("front/list")
	public void list(@RequestParam(defaultValue="1") int page, HttpServletResponse response) {
		PageRequest pageRequest = new PageRequest(3);
		pageRequest.setPageNo(page);
		pageRequest.setOrderBy("ctime");
		pageRequest.setOrderDir("desc");

		Page<Article> data = articleService.search2(pageRequest);

		String json = new JSON2(data).with(Dictionary.class, "itemValue")
					.with(AppUser.class, "fullname").toJson();
		ResponseUtils.renderJson(response, json);
	}
	
	@RequestMapping("front/message")
	public String message() {
		return "blog/message";
	}
	
	@RequestMapping("front/about")
	public String about() {
		return "blog/about";
	}
	
	@RequestMapping("front/comment")
	public String comment() {
		return "blog/comment";
	}
	
	@RequestMapping("front/view/{id}")
	public String view(@PathVariable Long id, Model model) {
		Article article = articleService.get(id);
		model.addAttribute("article", article);
		
		return "blog/details";
	}
	
	@ResponseBody
	@RequestMapping("chart1")
	public Msg chart1() {
		String[] arr = new String[] {"直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"};
		Map<String, Object> data = new HashMap<>();
		data.put("names", arr);
		
		List<Map<String, Object>> list = new ArrayList<>();
		for(String name : arr) {
			Map<String, Object> sub = new HashMap<>();
			sub.put("name", name);
			sub.put("value", RandomUtils.nextInt(20, 60));
			sub.put("my", "自定义_" + name);
			list.add(sub);
		}
		data.put("datas", list);
		
		return new Msg(true, 0, "chart1", data);
	}
	
	/**
	 * 图表展示
	 * */
	@ResponseBody
	@RequestMapping("chart2")
	public Msg chart2() {
		// 可以从数据库获得 label
		String[] arr = new String[] {"00:00", "01:15", "02:30", "03:45", "05:00", "06:15", "07:30", "08:45", "10:00", "11:15", "12:30", "13:45", "15:00", "16:15", "17:30", "18:45", "20:00", "21:15", "22:30", "23:45"};
		
		//double[] datas = new double[] {300, 280, 250, 260, 270, 300, 550, 500, 400, 390, 380, 390, 400, 500, 600, 750, 800, 700, 600, 400};
		List<Map<String, Object>> datas = new ArrayList<>();
		for(String name : arr) {
			Map<String, Object> sub = new HashMap<>();
			sub.put("name", name);
			sub.put("value", RandomUtils.nextInt(200, 800));
			sub.put("txt", "自定义_" + name);
			datas.add(sub);
		}
		
		Map<String, Object> data = new HashMap<>();
		data.put("names", arr);
		data.put("datas", datas);
		
		return new Msg(true, 0, "chart2", data);
	}
}
